// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief    DTI Parser
 *
 * @version  0.1
 * @author   wuxingchuang
 * @date     2025.06.23
 * @copyright Copyright (c) 2025
 */

#include "Import/BDFImport/BulkData/Dynamic/SPECSELParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "boost/algorithm/string.hpp"

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Dynamic/ISPECSELService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (SPECSEL);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

enum SPECSELKey
{
    SPECSELKEY_DTI     = 1,
    SPECSELKEY_SPECSEL = 2,
    SPECSELKEY_RECNO   = 3,
    SPECSELKEY_TYPE    = 5,
    SPECSELKEY_TID1    = 6,
    SPECSELKEY_DAMP1   = 7,
    // ...TIDi/DAMPi继续后推
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseSPECSEL (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                           std::vector<std::string>&                               dividedEntryInfo)
{
    int curKey = 1;

    SG::DataStructure::FEM::SPECSELData tempData;

    SG::DataStructure::FEM::SPECSELData::TABLESData curTable;

    bool hasTID = false;

    for (auto& curStrKey : dividedEntryInfo)
    {
        const bool isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curKey++;
            continue;
        }

        boost::trim (curStrKey);
        switch (curKey)
        {
        case SPECSELKEY_DTI:
            // 一般为"DTI"，可忽略或校验
            break;
        case SPECSELKEY_SPECSEL:
            break;
        case SPECSELKEY_RECNO:
            tempData.m_id = convertToI64(curStrKey);
            break;
        case SPECSELKEY_TYPE:
            strncpy (tempData.m_TYPE, curStrKey.c_str (), sizeof (tempData.m_TYPE) - 1);
            tempData.m_TYPE[sizeof (tempData.m_TYPE) - 1] = '\0';
            break;
        default:
            if ("ENDREC" == curStrKey)
            {
                // 结束标志，跳出循环
                break;
            }
            // TIDi DAMPi 成对解析
            if (((curKey - SPECSELKEY_TID1) % 2) == 0)
            {
                // TIDi
                curTable       = SG::DataStructure::FEM::SPECSELData::TABLESData ();
                curTable.m_TID = convertToI64 (curStrKey);
                hasTID         = true;
            }
            else
            {
                // DAMPi
                if (hasTID)
                {
                    curTable.m_DAMP = convertToReal (curStrKey);
                    tempData.m_TABLESList.push_back (curTable);
                    hasTID = false;
                }
            }
            break;
        }
        curKey++;
    }

    // domainId可按需要设置
    tempData.m_domainId = 0;

    return dbServiceFactory->get<ISPECSELService> ()->Add (tempData);
}